﻿using System;
using System.Collections.Generic;
using System.Data;
using Runda.EShop.Common.Utility;
using Runda.EShop.Common.Entity;
using Runda.EShop.Dal;


namespace Runda.EShop.Bll
{
    /// <summary>
    /// BLL Layer For dbo.Biz_Order_Process.
    /// </summary>
    public partial class BizOrderProcessBll 
    {
        public void InsertOrderProcess(OrderProcessWrapperEntity entity)
        {
            BizOrderProcessEntity procEntity = new BizOrderProcessEntity();
            procEntity.OrderID = new Guid(entity.OrderID);
            procEntity.InternalOrderCode = entity.OrderCode;
            procEntity.ProcessStatus = entity.ProcessStatus;
            procEntity.ProcessMemo = entity.ProcessMemo;
            procEntity.CarriageUrl = entity.ExpressUrl;
            procEntity.CarriageBy = entity.ExpressCompany;
            procEntity.DeliverCode = entity.DeliverCode;
            procEntity.ProcessTime = DateTime.Now;//Convert.ToDateTime(entity.ProcessTime);
            procEntity.CreatedAt = DateTime.Now;
            this.Insert(procEntity);

            //TODO:如果物流开始发货 则更新订单状态为"Delivery:订单发运安排中"，如果整个订单的物流已完成，则更新订单状态为  [EnumDesc("订单已交付")]Complete,
            //WMS:， 比如40 表示我的分配完成、60 是拣货完成、80是发运完成等等
            //№1Xuxl 16:07:04 或者不麻烦的话田工 判断下，完全发运 和订单完成 都属于 完全发运
            BizOrderBll bll = new BizOrderBll();
            if (entity.ProcessStatus == "分配完成")
            {
                //解决第一次分配完成WMS也传状态的bug。只有在订单状态为客户确认情况下才更新。第一次分配订单的状态在InsertOrderDetailPatch接口中更新。
                BizOrderEntity orderEntity = bll.Get<BizOrderEntity>(procEntity.OrderID);
                if (orderEntity.OrderStatus == (int)EnumHelper.OrderStatus.CustomerConfirm)
                {
                    bll.UpdateOrderStatus(new Guid(entity.OrderID), EnumHelper.OrderStatus.DeliveryProcessing);
                }
            }
            if (entity.ProcessStatus == "完全发运" || entity.ProcessStatus == "订单完成")
            {
                bll.UpdateOrderStatus(new Guid(entity.OrderID), EnumHelper.OrderStatus.DeliveryComplete);
            }
        }
    }
}

